<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'订单列表'),
);
$batch=array(
	array('type'=>'post','url'=>'jy_area_dian/orderfa/tostatus2/','title'=>'一键发货到自提点'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){	
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'dian_id >'=>0);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if($_RQ['dian_id']){
		$params['dian_id']=intval($_RQ['dian_id']);
		$return['params']['dian_id']=$_RQ['dian_id'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('order',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$order=pdo_get('jy_weishop_order',array('id'=>$item['orderid']),array('plugin_name','ordersn','realname','addressid','address'));
			$item['ordersn']=$order['ordersn'];
			$address=$order['address']?json_decode($order['address'],true):$order['addressid'];
			if($address){
				$item['realname']=$address['name']?:$order['realname'];
				$item['address']=model('core/member')->get_address_str($address);
			}
			if($order['plugin_name']=='jy_xiyi' && !$item['type']){
				$this->pdo_update('order',array('type'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
				$item['type']=1;
			}
			$item['goodsinfo']='';
			$item['goodsnum']=0;
			$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$item['orderid'],'uniacid'=>$_SESSION['uniacid']),array('goodsid','istaocan','optionid','price','total','goodssn','productsn'));
			foreach($ordergoods as &$gv){
				$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice','unit','image','title','merchid'));
				$goodsinfo['total']=$gv['total'];
				$goodsinfo['price']=$gv['price'];
				if($_SESSION['is_tomedia']){
					$goodsinfo['image']=$goodsinfo['image']?tomedia($goodsinfo['image']):'';
				}
				$goodsinfo['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$gv['goodsid']),'weight');
				//商品规格
				if($gv['optionid']){
					$options=pdo_get('jy_weishop_goods_option',array('id'=>$gv['optionid']),array('title','specs'));
					if($gv['istaocan']){
						$goodsinfo['selespename']=$options['title'];
					}else{
						$specs=$options['specs'];
						$specs=explode("_",$specs);
						foreach($specs as &$spec){
							$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
						}
						$goodsinfo['selespename']=implode("，",$specs);
					}
				}
				$item['goods'][] = $goodsinfo;
				$item['goodsinfo'] .=$goodsinfo['title'];
				if($goodsinfo['selespename']){
					$item['goodsinfo'] .='['.$goodsinfo['selespename'].']';
				}
				$item['goodsinfo'] .="(数量：".$goodsinfo['total'].")；";
			}
			$item['commission_money']=$this->pdo_getcolumn('commission',array('ordersn'=>$item['ordersn'],'uniacid'=>$_SESSION['uniacid']),'money');
			$item['commission_money']=$item['commission_money']?:'';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('type'=>'string_more','title'=>'订单ID',
			'data'=>array(
				array('title'=>'ID：','name'=>'orderid'),
				array('title'=>'','name'=>'ordersn'),
				)
			),
			array('type'=>'string_more','title'=>'订单金额',
			'data'=>array(
				array('title'=>'金额：','name'=>'order_price'),
				array('title'=>'佣金：','name'=>'commission_money'),
				)
			),
			array('name'=>'dian_id','title'=>'网点','type'=>'select','options'=>'dians'),
			array('name'=>'address','title'=>'地址','type'=>'string'),
			array('name'=>'goodsinfo','title'=>'商品详情','type'=>'string'),
			array('name'=>'type','title'=>'订单模式','type'=>'radio','options'=>'order_types'),
			array('name'=>'sendtype','title'=>'配送方式','type'=>'radio','options'=>'sendtypes'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'setdian','title'=>'指派订单','show'=>array('status'=>array(0,1,4,5,6,14,15,16,17,18,19,20))),
			array('type'=>'post','op'=>'tostatus2','title'=>'发货到网点','show'=>array('status'=>1,'dian_id >'=>0)),
			array('type'=>'post','op'=>'tostatus18','title'=>'已收到网点的货','show'=>array('status'=>17)),
			array('type'=>'post','op'=>'tostatus19','title'=>'处理完成','show'=>array('status'=>18)),
			array('type'=>'post','op'=>'tostatus2','title'=>'网点已收货','show'=>array('status'=>array(19,20))),
		),'batch'=>$batch),
		'search'=>array(
			array('type'=>'choose','width'=>18,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
			array('name'=>'dian_id','width'=>15,'title'=>'网点','type'=>'select','options'=>'dians'),
			array('name'=>'createtime','width'=>15,'title'=>'下单时间','type'=>'time_range')
		),
	);
	$return['status']=$this->order_status;
	$return['sendtypes']=$this->send_types;
	$return['order_types']=$this->order_types;
	$return['dians']=$this->getall('agent',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setdian'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$post['dian_id']){
			exi('请选择自提点！','error');
		}
		$update=array('dian_id'=>intval($post['dian_id']));
		if(!$post['status']){
			$update['status']=1;
		}
		$this->update('order',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('设置成功！','','list');
	}
	$item=$this->get('order',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定订单不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'设置订单网点',
		'edit'=>array(
			array('name'=>'dian_id','title'=>'网点','type'=>'select','options'=>'dians'),
		),
	);
	$return['dians']=$this->getall('agent',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	exi($return);
}
if($_RQ['op']=='tostatus18'){
	$logid=intval($_RQ['id']);
	if(!$logid){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->pdo_update('order',array('status'=>18),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
	$order=$this->pdo_get('order',array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
	pdo_update('jy_weishop_order',array('status'=>2),array('id'=>$order['orderid']));
	$this->model('notice')->notice_upstatus($logid);
	exi('操作成功!');
}
if($_RQ['op']=='tostatus19'){
	$logid=intval($_RQ['id']);
	if(!$logid){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=$this->pdo_get('order',array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
	$status=$order['dian_id']?20:19;
	$this->pdo_update('order',array('status'=>$status),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
	$this->model('notice')->notice_upstatus($logid);
	exi('操作成功!');
}
if($_RQ['op']=='tostatus5'){
	$logid=intval($_RQ['id']);
	if(!$logid){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->pdo_update('order',array('status'=>5),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
	$this->model('notice')->notice_upstatus($logid);
	exi('操作成功!');
}
if($_RQ['op']=='tostatus2'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择订单','error');
	}
	foreach($ids as $id){
		$logid=intval($id);
		if($logid){
			$this->pdo_update('order',array('status'=>2),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
			$order=$this->pdo_get('order',array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
			pdo_update('jy_weishop_order',array('status'=>2),array('id'=>$order['orderid'],'uniacid'=>$_SESSION['uniacid']));
			$this->model('notice')->notice_upstatus($logid);
		}
	}
	exi('操作成功!');
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if($_RQ['dian_id']){
		$params['dian_id']=intval($_RQ['dian_id']);
		$return['params']['dian_id']=$_RQ['dian_id'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getall('jy_area_dian_order',$params);
	foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['nickname']=$item['member']['nickname'];
			$order=pdo_get('jy_weishop_order',array('id'=>$item['orderid']),array('plugin_name','ordersn','realname','addressid','address'));
			$item['ordersn']=$order['ordersn'];
			$address=$order['address']?json_decode($order['address'],true):$order['addressid'];
			if($address){
				$item['realname']=$address['name']?:$order['realname'];
				$item['address']=model('core/member')->get_address_str($address);
			}
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['storetitle']=pdo_getcolumn('jy_area_dian_agent',array('id'=>$item['dian_id']),'title');
			$item['type']=$this->order_types[$item['type']];
			$item['sendtype']=$this->send_types[$item['sendtype']];
			$item['goodsinfo']='';
			$item['goodsnum']=0;
			$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$item['orderid'],'uniacid'=>$_SESSION['uniacid']),array('goodsid','istaocan','optionid','price','total','goodssn','productsn'));
			foreach($ordergoods as &$gv){
				$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice','unit','image','title','merchid'));
				$goodsinfo['total']=$gv['total'];
				$goodsinfo['price']=$gv['price'];
				if($_SESSION['is_tomedia']){
					$goodsinfo['image']=$goodsinfo['image']?tomedia($goodsinfo['image']):'';
				}
				$goodsinfo['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$gv['goodsid']),'weight');
				//商品规格
				if($gv['optionid']){
					$options=pdo_get('jy_weishop_goods_option',array('id'=>$gv['optionid']),array('title','specs'));
					if($gv['istaocan']){
						$goodsinfo['selespename']=$options['title'];
					}else{
						$specs=$options['specs'];
						$specs=explode("_",$specs);
						foreach($specs as &$spec){
							$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
						}
						$goodsinfo['selespename']=implode("，",$specs);
					}
				}
				$item['goods'][] = $goodsinfo;
				$item['goodsinfo'] .=$goodsinfo['title'];
				if($goodsinfo['selespename']){
					$item['goodsinfo'] .='['.$goodsinfo['selespename'].']';
				}
				$item['goodsinfo'] .="(数量：".$goodsinfo['total'].")；";
				$item['status']= $this->order_status[$item['status']];
			}
	}
	$header=array(
		'nickname'=>'买家昵称',
		'realname'=>'联系人',
		'storetitle'=>'所属网点',
		'address'=>'地址',
		'ordersn'=>'订单号',
		'goodsinfo'=>'商品详情',
		'order_price'=>'金额',
		'commission_money'=>'佣金',
		'sendtype'=>'配送方式',
		'status'=>'订单状态',
		'createtime'=>'创建时间',
	);
	cfc('tools')->export2excel($header,$list,'订单列表');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$this->delete('order',array('id'=>$id));	
	}
	if(!$id){
		exi('请选择订单！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('order',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>